/**
 * 角色权限关联控制器
 * 作者：GodMainCode
 * 创建时间：2024-01-17
 * 修改时间：2024-01-17
 * 修改人：GodMainCode
 */

import { 
  Controller, 
  Post, 
  Body, 
  Param, 
  Delete,
  HttpCode,
  HttpStatus,
  UseGuards
} from '@nestjs/common';
import { 
  ApiTags, 
  ApiOperation, 
  ApiResponse,
  ApiParam,
  ApiBody,
  ApiProperty
} from '@nestjs/swagger';
import { RolePermissionService } from './role-permission.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RequirePermissions } from '../auth/decorators/permissions.decorator';
import { IsArray, IsInt } from 'class-validator';

class AssignPermissionsDto {
  @ApiProperty({ description: '权限ID列表', type: [Number] })
  @IsArray()
  @IsInt({ each: true })
  permissionIds: number[];
}

@ApiTags('角色权限管理')
@UseGuards(JwtAuthGuard)
@Controller('role-permission')
export class RolePermissionController {
  constructor(private readonly rolePermissionService: RolePermissionService) {}

  @Post(':roleId/permissions')
  @HttpCode(HttpStatus.OK)
  @RequirePermissions('role:assignPermission')
  @ApiOperation({ summary: '为角色分配权限' })
  @ApiParam({ name: 'roleId', description: '角色ID' })
  @ApiBody({ type: AssignPermissionsDto })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '权限分配成功' 
  })
  async assignPermissions(
    @Param('roleId') roleId: string,
    @Body() assignPermissionsDto: AssignPermissionsDto,
  ) {
    await this.rolePermissionService.assignPermissions(+roleId, assignPermissionsDto.permissionIds);
    return { message: '权限分配成功' };
  }

  @Delete(':roleId/permissions')
  @HttpCode(HttpStatus.OK)
  @RequirePermissions('role:removePermission')
  @ApiOperation({ summary: '移除角色的权限' })
  @ApiParam({ name: 'roleId', description: '角色ID' })
  @ApiBody({ type: AssignPermissionsDto })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '权限移除成功' 
  })
  async removePermissions(
    @Param('roleId') roleId: string,
    @Body() assignPermissionsDto: AssignPermissionsDto,
  ) {
    await this.rolePermissionService.removePermissions(+roleId, assignPermissionsDto.permissionIds);
    return { message: '权限移除成功' };
  }
} 